package com.rampage.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 配置工具类
 * Created by zipeng on 2016/3/29.
 */
public class ConfigUtils {

    private static Map<String, String> mCache = new HashMap<>();

    private static String loadPropery(String name) {
        String value = null;
        InputStreamReader reader = null;
        try {
            InputStream in = ConfigUtils.class.getClassLoader().getResourceAsStream("config.properties");
            reader = new InputStreamReader(in, "UTF-8");
            Properties props = new Properties();
            props.load(reader);
            value = props.getProperty(name, null);
        } catch (Exception e) {

        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return value;
    }

    /**
     * 获取网站名称
     *
     * @return
     */
    public static String getSiteName() {
        return getPropery("site_name");
    }

    /**
     * 获取config属性
     *
     * @param name
     * @return
     */
    public static String getPropery(String name) {
        if (mCache.containsKey(name)) {
            return mCache.get(name);
        } else {
            String prop = loadPropery(name);
            mCache.put(name, prop);
            return prop;
        }
    }
}
